﻿using System;
using System.Collections.Generic;
using System.Linq;

public class OrderDetails
{
    public string ProductName { get; set; }
    public decimal Quantity { get; set; }
    public decimal Price { get; set; }//这是单价

    public override bool Equals(object obj)
    {
        if (obj is OrderDetails other)
        {
            return ProductName == other.ProductName && Quantity == other.Quantity && Price == other.Price;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(ProductName, Quantity, Price);
    }

    public override string ToString()
    {
        return $"Product: {ProductName}, Quantity: {Quantity}, Price: {Price}";
    }
}

public class Order
{
    public int OrderId { get; set; }
    public string Customer { get; set; }
    public List<OrderDetails> Details { get; set; } = new List<OrderDetails>();
    public string DetailsSummary => Details.Any() ?
        string.Join(", ", Details.Select(d => $"{d.ProductName}(×{d.Quantity})")) :
        "无商品";
    public decimal TotalAmount => Details.Sum(d => d.Quantity * d.Price);

    public override bool Equals(object obj)
    {
        if (obj is Order other)
        {
            return OrderId == other.OrderId && Customer == other.Customer && Details.SequenceEqual(other.Details);
        }
        return false;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(OrderId, Customer, Details);
    }

    public override string ToString()
    {
        return $"Order ID: {OrderId}, Customer: {Customer}, Total Amount: {TotalAmount}\n\nDetails:\n{string.Join("\n", Details)}";
    }
}

public class OrderService
{
    private List<Order> orders = new List<Order>();

    public void AddOrder(Order order)
    {
        if (orders.Any(o => o.OrderId == order.OrderId))
        {
            throw new InvalidOperationException("Order with the same ID already exists.");
        }
        orders.Add(order);
    }

    public void RemoveOrder(int orderId)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null)
        {
            throw new InvalidOperationException("Order not found.");
        }
        orders.Remove(order);
    }

    public void UpdateOrder(Order order)
    {
        var existingOrder = orders.FirstOrDefault(o => o.OrderId == order.OrderId);
        if (existingOrder == null)
        {
            throw new InvalidOperationException("Order not found.");
        }
        orders.Remove(existingOrder);
        orders.Add(order);
    }

    public List<Order> QueryOrders(OrderQueryParameters parameters)
    {
        IQueryable<Order> query = orders.AsQueryable();

        // 按订单ID
        if (parameters.OrderId.HasValue)
            query = query.Where(o => o.OrderId == parameters.OrderId);

        // 按客户名（模糊匹配）
        if (!string.IsNullOrEmpty(parameters.Customer))
            query = query.Where(o => o.Customer.Contains(parameters.Customer));

        // 按金额范围
        if (parameters.MinAmount.HasValue)
            query = query.Where(o => o.TotalAmount >= parameters.MinAmount);
        if (parameters.MaxAmount.HasValue)
            query = query.Where(o => o.TotalAmount <= parameters.MaxAmount);

        return query.ToList();
    }

    public class OrderQueryParameters
    {
        public int? OrderId { get; set; }
        public string Customer { get; set; }
        public decimal? MinAmount { get; set; }
        public decimal? MaxAmount { get; set; }

        public override string ToString()
        {
            var conditions = new List<string>();

            if (OrderId.HasValue)
                conditions.Add($"订单ID: {OrderId}");

            if (!string.IsNullOrEmpty(Customer))
                conditions.Add($"客户名称: {Customer}");

            if (MinAmount.HasValue || MaxAmount.HasValue)
            {
                var range = MinAmount.HasValue ? $"≥{MinAmount}" : "";
                range += MaxAmount.HasValue ? $"≤{MaxAmount}" : "";
                conditions.Add($"金额范围: {range}");
            }

            return conditions.Any()
                ? "当前搜索条件:\n" + string.Join("\n", conditions)
                : "当前无搜索条件";
        }
    }

    public List<Order> GetAllOrders()
    {
        return orders.OrderBy(o => o.OrderId).ToList();
    }

    public void SortOrders(Comparison<Order> comparison = null)//默认是null，按照ID排序，可以自定义排序方法
    {
        if (comparison == null)
        {
            orders.Sort((o1, o2) => o1.OrderId.CompareTo(o2.OrderId));
        }
        else
        {
            orders.Sort(comparison);
        }
    }
}